<?php
$this->pageTitle = 'Manage Menus';
$this->breadcrumbs = array(
    'Manage Menus',
);

Util::registerScript('updateStatus', "
    $('a.update-status').live('click', function() {
        $.fn.yiiGridView.update('menu-grid', {
                type: 'POST',
                    url: $(this).attr('href'),
                    success: function() {
                        $.fn.yiiGridView.update('menu-grid');
                    },
            });
            return false;
    });
");
?>
<div class="heading">
    <h1><img src="<?php echo Util::themeUrl() ?>/images/header.ico" /> Manage Menus</h1>

    <div class="controls">
        <?php
        echo CHtml::button('Create', array('class' => 'controls', 'id' => 'create-menu'));
        echo CHtml::button('Delete', array('class' => 'controls', 'id' => 'delete-menu'));
        ?>
    </div>
</div>

<div class="content">

    <?php
    $this->widget('zii.widgets.grid.CGridView', array(
        'id' => 'menu-grid',
        'dataProvider' => $model->search(),
        'filter' => $model,
        'cssFile' => Util::themeUrl() . '/css/gridview.css',
        'htmlOptions' => array('class' => 'grid-view clear'),
        'afterAjaxUpdate' => 'js:function(id,data){$.bind_data()}',
        'columns' => array(
            array(
                'class' => 'CCheckBoxColumn',
                'selectableRows' => 2,
                'id' => 'chk',
                'value' => '$data->id',
            ),
            array(
                'name' => 'id',
                'htmlOptions' => array('width'=>80)
            ),
            array(
                'name' => 'name',
                'type' => 'html',
                'value' => 'str_repeat("&nbsp;", (count(explode("/", $data->level)) - 1)*10) . $data->name',
                'htmlOptions' => array('style' => 'min-width: 300px')
            ),
            'type_id' => array(
                'name' => 'type_id',
                'filter' => array(Menu::MAIN_MENU => 'Main Menu', Menu::ADMIN_MENU => 'Admin Menu', Menu::USER_MENU => 'User Main'),
                'value' => '$data->type->name',
            ),
            'url',
            'weight',
            array(
                'name' => 'status',
                'htmlOptions' => array('width' => 80),
                'type' => 'raw',
                'value' => 'CHtml::link(CHtml::image(Util::themeUrl(). "/images/" . (($data->status==1)?"tick_circle.png":"cross_circle.png")), array("update_status", "id"=>$data->id), array("class" => "update-status"))',
//                'filter' => array(1 => 'Active', 0 => 'Inactive')
            ),
            array(
                'name' => 'visibleExpression',
                'visible' => Util::user()->id == 1,
            ),
            array(
                'class' => 'CButtonColumn',
                'viewButtonImageUrl' => false,
                'updateButtonImageUrl' => false,
                'deleteButtonImageUrl' => false,
                'template' => '[{view}] [{update}]'
            ),
        ),
    ));
    ?>
</div>

<script  type="text/javascript">
    $(document).ready(function() {
        $. bind_data = function(){
            $(".grid-view a.view").click(function() {
                var url = $(this).attr('href') + '?ajax=true';
                $.fancybox.open({
                    href : url,
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('menu-grid')
                    }
                });
                return false;
            });

            $(".grid-view a.update").click(function() {
                var url = $(this).attr('href') + '?ajax=true';
                $.fancybox.open({
                    href : url,
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('menu-grid')
                    }
                });
                return false;
            });
        }

        // bind data
        $. bind_data();

        $("#create-menu").click(function() {
            $.fancybox.open({
                href : '<?php echo $this->createUrl('create', array('ajax' => true)) ?>',
                type : 'iframe',
                afterClose: function(){
                    $.fn.yiiGridView.update('menu-grid')
                }
            });
            return false;
        });

        $('#delete-menu').live('click', function(){
            var count=0;
            var params = 'YII_CSRF_TOKEN=<?php echo Yii::app()->request->csrfToken; ?>';
            $("input[name='chk[]']").each(function(){
                if($(this).is(":checked")){
                    count++;
                    params += '&chk[]=' + $(this).val();
                }
            });
            if(count >= 1 ){
                if(!confirm("Delete selected records"))
                    return false;
            }else{
                alert("No checkbox is selected");
                return false;
            }

            $.ajax({
                type: "POST",
                url: '<?php echo $this->createUrl('delete', array('ajax' => true)) ?>',
                data: params,
                success: function(data) {
                    $.fn.yiiGridView.update('menu-grid');
                } //success
            });//ajax
            return false;
        });
    });
</script>